#include "PRGXMLPacketer.h"

PRGXMLPacketer::PRGXMLPacketer()
{
}


PRGXMLPacketer::~PRGXMLPacketer()
{
}

void PRGXMLPacketer::AddType(char *vType)
{
	if(vType)	AddField("Type",vType);
}

void PRGXMLPacketer::AddResult(int value, char *desc)
{
	if(desc) {
		char buffer[10];
		sprintf(buffer,"%d",value);
		AddField("Code",buffer);
		AddField("CodeDesc",desc);
	}
}

void PRGXMLPacketer::AddFileName(char *Value)
{
	if(Value) {
		AddField("AssetPath",Value);
	}
}

void PRGXMLPacketer::Packet(char *vBuff, unsigned int &vLen)
{
	if(vBuff) {
		m_Content.insert(0,"<MSBC>");
		m_Content.append("</MSBC>");

		//calc size field
		vLen = m_Content.length() +4;
		char cLen[5];
		cLen[0] = 0xff&(vLen >>24);
		cLen[1] = 0xff&(vLen >>16);
		cLen[2] = 0xff&(vLen >>8);
		cLen[3] = 0xff&vLen;
		//add size field
		::memcpy(vBuff,cLen,4);
		//add content
		::memcpy(vBuff+4,m_Content.c_str(),vLen-4);
	}
}

void PRGXMLPacketer::AddField(char *FieldStr, char *FieldValue)
{
	if(FieldStr && FieldValue ) {
		char buffer[100];
		sprintf(buffer,"<%s>%s</%s>",FieldStr,FieldValue,FieldStr);
		m_Content += buffer;
	}
}
